package mtt.view.calendar;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import mtt.control.DisplayController;
import mtt.control.CalendarController;
import mtt.model.calendar.Day;
import mtt.model.calendar.Week;
import mtt.view.IMttDisplayAble;

/**
 * Diese View zeigt alle Wochentage
 * 
 * @author hyphen
 */
public class WeekView implements IMttDisplayAble {

	private static final Command commandBack = new Command("zurück",
			Command.BACK, 0);

	private static final Command commandOk = new Command("Ok", Command.OK, 0);

	private static final List list = new List(null, List.IMPLICIT);

	private static final WeekView weekView = new WeekView();

	/**
	 * enthält die aktuelle Woche
	 */
	private Week currentWeek;
	
	/**
	 * Factory Methode für die WeekView instanz
	 * 
	 * @return
	 */
	public static final WeekView getInstace() {
		return weekView;
	}

	public void commandAction(Command command) {

		if (command == WeekView.commandOk) {
			/*
			 * welcher Tag wurde ausgewählt
			 */
			int selectedIndex = list.getSelectedIndex();
			
			/*
			 * Tag anzeigen
			 */
			DisplayController displayController = DisplayController.getInstance();
			
			displayController.showDayView(this.currentWeek.getDay(selectedIndex));
		}

	}

	public Displayable getDisplayable() {
		return list;
	}

	/**
	 * initialisiert die View mit der anzuzeigenden Woche
	 * 
	 * @param week
	 */
	public void setWeek(Week week) {
		this.currentWeek = week;
		
		/*
		 * vorhandene elemente der Liste löschen
		 */
		list.deleteAll();
		
		CalendarController calendarController = CalendarController
				.getInstance();

		list.setTitle("Wochenansicht: "
				+ calendarController.getStringOfWeek(week));

		/*
		 * Wochentage einfügen
		 */
		list.insert(Day.index_MONDAY, Day.getStringOfDayId(Day.index_MONDAY),
				null);
		list.insert(Day.index_TUESDAY, Day.getStringOfDayId(Day.index_TUESDAY),
				null);
		list.insert(Day.index_WEDNESDAY, Day
				.getStringOfDayId(Day.index_WEDNESDAY), null);
		list.insert(Day.index_THURSDAY, Day
				.getStringOfDayId(Day.index_THURSDAY), null);
		list.insert(Day.index_FRIDAY, Day.getStringOfDayId(Day.index_FRIDAY),
				null);
		list.insert(Day.index_SATURDAY, Day
				.getStringOfDayId(Day.index_SATURDAY), null);
		list.insert(Day.index_SUNDAY, Day.getStringOfDayId(Day.index_SUNDAY),
				null);

		/*
		 * Buttons einfügen
		 */
		list.addCommand(commandBack);
		list.addCommand(commandOk);

		/*
		 * Listener anhängen
		 */
		list.setCommandListener(DisplayController.getInstance());
	}
}
